package com.ys.timer;

import com.ys.entity.finance.TcwDepositPlatform;
import com.ys.entity.finance.TcwDepositPlatformAccount;
import com.ys.entity.purchase.TcgPurchaseOrderPay;
import com.ys.entity.sys.TbStaticConfig;
import com.ys.service.finance.TcwDepositPlatformAccountService;
import com.ys.service.finance.TcwWanliFinanceService;
import com.ys.service.finance.TcwWanliPrepaymentPoolingService;
import com.ys.service.log.ApiLogService;
import com.ys.vo.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;

@Controller
public class TimerWanliDaily {

	@Autowired
	private ApiLogService apiLogService;

	@Autowired
	private TcwWanliPrepaymentPoolingService tcwWanliPrepaymentPoolingService;

	@Autowired
	private TcwWanliFinanceService tcwWanliFinanceService;

	@Autowired
	private TcwDepositPlatformAccountService tcwDepositPlatformAccountService;



	/**
	 * 获取万里汇流水明细(集成商可调用此接口获取交易账单)
	 */
		@Scheduled(cron = "0 0 0 * * * ")
	public void inquiryStatementList() {
		try {
			//查询系统数据
			TbStaticConfig tbStaticConfig = new TbStaticConfig().setKeyOne("wanli-mid").setKeyTwo("wanli-mid-cs").queryFirst();
			//查询莫程收款平台账号管理
			TcwDepositPlatform tcwDepositPlatform = new TcwDepositPlatform().setMid(tbStaticConfig.getName()).queryFirst();
			ApiResponse apiResponse = tcwWanliPrepaymentPoolingService.inquiryStatementList(tcwDepositPlatform);
			if (apiResponse.getCode() == 500) {
				apiLogService.addApiLog("Wanli", "inquiryStatementList", "ECP_API定时获取万里汇接口获取店铺信息以出错，：", "Mid:" + tcwDepositPlatform.getMid(), apiResponse.getCode(), apiResponse.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 可调用inquiryBalance接口查询莫程账户余额。
	 * 2024-09-02 每天00:18:00刷万里汇数据
	 */
	@Scheduled(cron = "0 18 * * * * ")
	public void inquiryBalanceMc() {
		try {
			ApiResponse apiResponse = tcwWanliFinanceService.inquiryBalance();
			if (apiResponse.getCode() == 500) {
				apiLogService.addApiLog("Wanli", "inquiryBalance", "ECP_API定时获取万里汇接口查询账户余额出错，：" ,"账户:莫程" , apiResponse.getCode(), apiResponse.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 集成商可调用此接口获取店铺信息以及店铺关联账号的信息
	 */
	@Scheduled(cron = "0 0 7 * * * ")
	public void inquiryStore() {
		try{
			Date date = new Date();
			//查询收款平台存款账户信息
			List<TcwDepositPlatformAccount> tcwDepositPlatformAccountList = new TcwDepositPlatformAccount().queryList();
			for (TcwDepositPlatformAccount tcwDepositPlatformAccount : tcwDepositPlatformAccountList){
				ApiResponse apiResponse = tcwDepositPlatformAccountService.inquiryStore(tcwDepositPlatformAccount,date);
				if (apiResponse.getCode() == 500) {
					apiLogService.addApiLog("Wanli", "inquiryStore", "ECP_API定时获取万里汇接口获取店铺信息以出错，：" ,"账户:"+tcwDepositPlatformAccount.getVyhzhmc() , apiResponse.getCode(), apiResponse.getMessage());
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 通过万里汇流水明细(付款状态更新1688订单对应的手续费)
	 */
	@Scheduled(cron = "0 0/1 * * * ? ")
	public void inquiryStatementListPayment() {
		try {
			//查询订单状态已付款/手续费并且是空的数据
			Integer count  = new TcgPurchaseOrderPay()
					.addLeftJoin("TCG_PurchaseByOrder B ON B.Bid= A.PurchaseOrderBid ")
					.setPayType(1)//1.跨境宝
					.where("B.OrderStatus = 7 AND A.chargeTrouble IS NULL AND A.chargeTroubleWf IS NULL")
					.count();
			if (count != 0){
				//查询系统数据
				TbStaticConfig tbStaticConfig = new TbStaticConfig().setKeyOne("wanli-mid").setKeyTwo("wanli-mid-cs").queryFirst();
				//查询莫程收款平台账号管理
				TcwDepositPlatform tcwDepositPlatform = new TcwDepositPlatform().setMid(tbStaticConfig.getName()).queryFirst();
				ApiResponse apiResponse = tcwWanliPrepaymentPoolingService.inquiryStatementListPayment(tcwDepositPlatform);
				if (apiResponse.getCode() == 500) {
					apiLogService.addApiLog("Wanli", "inquiryStatementListPayment", "ECP_API定时获取万里汇流水明细(付款状态更新1688订单对应的手续费)以出错，：", "Mid:" + tcwDepositPlatform.getMid(), apiResponse.getCode(), apiResponse.getMessage());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
